package com.bor.gb.core.repo;

import com.bor.gb.core.exception.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.MongoRepository;


/**
 * Created by bor on 2/8/15.
 */
public abstract class CustomCommonImpl<T> implements CustomCommon<T>{

    @Autowired
    private MongoRepository<T, String> repository;

    private final Class<T> clsInst;

    public CustomCommonImpl(Class<T> cls) {
        this.clsInst = cls;
    }

    /**
     * getById by Mongo default ID
     * @param id
     */
    public T getById(String id){
        T entity = repository.findOne(id);

        if( entity == null){
            throw new EntityNotFoundException("Can not fetch entity: " + clsInst.getSimpleName() + " by id: " + id);
        }
        return entity;
    }
}
